package com.geekstudy.week7.entity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicLong;

public class Week7Work6 {

    static AtomicLong atomicLong = new AtomicLong(100000);

    public String snowId() {
        int hostId = 2689;
        return snowId(hostId);
    }

    public String snowId(int hostId) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Date currentDate = new Date();
        String current = simpleFormat.format(currentDate);

        long sequence = atomicLong.incrementAndGet();
        int random = ThreadLocalRandom.current().nextInt(10000, 99999);

        // 17位时间戳 + 机器码 + 6位机器号 + 5位随机码
        String snowId = "" + current + hostId + sequence + random;
        return snowId;
    }

    public static void main(String[] args) {
        Week7Work6 work6 = new Week7Work6();

        String id1 = work6.snowId();
        String id2 = work6.snowId();
        System.out.println(" " + id1);
        System.out.println(" " + id2);

        int hostId = 1001;
        String id3 = work6.snowId(hostId);
        String id4 = work6.snowId(hostId);
        System.out.println(" " + id3);
        System.out.println(" " + id4);


    }
}
